package com.xln.util;

import com.xln.configuration.JWTProperties;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Token {

    @Resource
    private JWTProperties jwtProperties;
    public static String createToken(String name,String password){
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, JWTProperties.adminSecret)
                .addClaims(new HashMap<String, Object>(){{
                    put("username",name);
                    put("password",password);
                }})
                .setExpiration(new Date(System.currentTimeMillis() + JWTProperties.adminExpiration))
                .compact();
        return token;
    }

    public static Map parseToken(String token){
        Map<String, Object> claims = Jwts.parser().setSigningKey(JWTProperties.adminSecret).parseClaimsJws(token).getBody();
        return claims;
    }
}
